<?php

class ProformaController extends CI_Controller {

    private $layout;

    public function __construct() {
        parent::__construct();
        $this->load->model("proformaModel", "proforma");
        $this->load->model("piezasModel", "piezas");
        $this->layout['librerias'] = $this->load->view(TMPL_LIBRERIA, "", true);
        $this->layout['menu'] = $this->load->view(TMPL_MENU, "", true);
        $this->layout['pie'] = $this->load->view(TMPL_PIE, "", true);
    }

    public function index() {
        $data['menu'] = $this->load->view('proforma/menu', '', true);
        $data['almacenes'] = array();
        $this->layout['contenido'] = $this->load->view('proforma/index', $data, true);
        $this->load->view(TMPL_GENERAL, $this->layout);
    }

    public function crear() {
        $data['menu'] = $this->load->view('proforma/menu', '', true);
//        $data['herramientas'] = json_decode($this->herramienta->listar_herramientas());
//        $data['almacenes'] = json_decode($this->almacen->listar_almacenes());
//        $this->form_validation->set_rules('almacen', 'almacen', 'required');
//        $this->form_validation->set_rules('producto', 'producto', 'required');

        if ($this->form_validation->run() == FALSE) {
            $this->layout['contenido'] = $this->load->view('proforma/create', $data, true);
            $this->load->view(TMPL_GENERAL, $this->layout);
        } else {
            
        }
    }

    public function guardar_pedido() {
        $productos = $this->input->post("productos");
        $cantidades = $this->input->post("cantidades");
        $almacen = $this->input->post("almacen");
        $items['productos'] = $productos;
        $items['cantidades'] = $cantidades;
        $items['almacen'] = $almacen;

        return $this->pedido->guardar_pedido($items);
    }

    public function buscar() {
        $cadena = $this->input->post("cadena", TRUE);
        // limpio la palabra que se busca
        $search = trim($cadena);
        $data['piezas']=json_decode($this->piezas->search($search));
        $this->load->view("proforma/buscar_ajax",$data);
        
    }
    public function buscar_pieza() {
        $idPieza = $this->input->post("idPieza", TRUE);
        echo $this->piezas->select_piezas($idPieza);
//        $data['pieza']=json_decode($this->piezas->select_piezas($idPieza));
//        $this->load->view("proforma/buscar_ajax",$data);
        
    }

}

?>
